let express = require("express")();

let mysql = require('mysql');

let port = 8080;
express.all("/*", function(req, res, next) {
  // 跨域处理
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By", ' 3.2.1');
  res.header("Content-Type", "application/json;charset=utf-8");
  next(); // 执行下一个路由
})

// 规划一个数据库链接
let sql = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '123456',
  database : 'student',
  timezone: '08:00'
//   timezone : "08:00"
});

// 尝试链接
sql.connect();
express.get("/getlist",(request,response)=>{
	sql.query(`SELECT id,name FROM student8`,(error,data)=>{
    if(error){
      console.log("error");
      response.send("error")
    }
    else{
      console.log(data);
      response.send(data)
    }
	
	})
})
express.get("/getnews",(request,response)=>{
  let id=request.query.id;
  sql.query(`SELECT * FROM student8 WHERE id=${id}`,(error,data)=>{
    if(error){
      console.log(error);
      response.send("error");
    }
    else{
      response.send(data[0])
    }
  })
})
// sql.query(`SELECT id,title FROM student7`,(error,data)=>{
//     console.log(data)
// })





// 如果接收到来自前端的静态文件请求的话，我该去哪里去给他找文件
// express.use(expressStatic(__dirname + '/static'));
console.log(`running at ${port}`)
express.listen(port)